package org.caowei.samples.Utils;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.caowei.samples.vo.UserVO;

public class ExcelUtils {
	
	public static final String OFFICE_EXCEL_2003_POSTFIX = ".xls";
	public static final String OFFICE_EXCEL_2007_POSTFIX = ".xlsx";

	//根据InputStream和Excel类型创建Excel Workbook
	public static Workbook openWorkbook(InputStream is,String postfix){
		Workbook hssfWorkbook = null;
		try{
			if(OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
				hssfWorkbook = new HSSFWorkbook(is);  //建立新Workbook对象
			}else if(OFFICE_EXCEL_2007_POSTFIX.equals(postfix)){
				hssfWorkbook = new XSSFWorkbook(is);  //建立新Workbook对象
			}
			
		}catch (Exception e) {
			if(e instanceof NotOLE2FileException){
				return null;
			}
			e.printStackTrace();
		}
		return hssfWorkbook;
	}
	
	//获取指定Excel表格的内容
	public static List<UserVO> getStudentListByExcel(Workbook wk){
		final String GENDER_MALE_STRING = "男",GENDER_FEMALE_STRING = "女";
		final Integer GENDER_MALE_INTEGER = 0,GENDER_FEMALE_INTEGER = 1;
		List<UserVO> resultList = new ArrayList<UserVO>();
		Sheet sheet = wk.getSheetAt(0);
		int maxCellNubmer = 0;
		int maxRowNubmer = sheet.getLastRowNum();
		UserVO vo = null;
		for(int currentRowNumber = 1;currentRowNumber <=maxRowNubmer;currentRowNumber ++){
			Row row = sheet.getRow(currentRowNumber);
			maxCellNubmer = row.getLastCellNum();
			vo = new UserVO();
			for(int currentCellNumber = 0;currentCellNumber < maxCellNubmer;currentCellNumber ++){
				Cell cell = row.getCell(currentCellNumber);
				switch(currentCellNumber){
				case 0:
					//获取学号列数据
					vo.setUserId((String) getCellValue(cell));
					break;
				case 1:
					//获取姓名列数据
					vo.setUserName((String) getCellValue(cell));
					break;
				case 2:
					//获取密码列数据
					vo.setPassword((String) getCellValue(cell));
					break;
				case 3:
					//获取性别列数据 
					String gender = (String) getCellValue(cell);
					if(GENDER_MALE_STRING.equals(gender)){
						vo.setGender(GENDER_MALE_INTEGER);
					}else if(GENDER_FEMALE_STRING.equals(gender)){
						vo.setGender(GENDER_FEMALE_INTEGER);
					}
					break;
				case 4:
					//获取电话列数据
					vo.setTel((String) getCellValue(cell));
					break;
				case 5:
					//获取地址列数据
					vo.setAddress((String) getCellValue(cell));;
					break;
					//获取邮件列数据
				case 6:
					vo.setEmail((String) getCellValue(cell));
				
				}
			}
			resultList.add(vo);
		}
		return resultList;
	}
	
	
	
	@SuppressWarnings("static-access")
	private static String getCellValue(Cell xssfRow) {
		if (xssfRow == null) {
			return "";
		}
		if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
			return String.valueOf(xssfRow.getBooleanCellValue());
		} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
			DecimalFormat df = new DecimalFormat("0");
			return df.format(xssfRow.getNumericCellValue());
			// return String.valueOf(xssfRow.getNumericCellValue());
		} else {
			return String.valueOf(xssfRow.getStringCellValue());
		}
	}
	
}
